//
//  HZLandscapeChartView.m
//  Stock_Health
//
//  Created by MayerF on 2017/8/1.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZLandscapeChartView.h"
#import "HZLandscapeSummaryView.h"
#import "MYRadioGroup.h"
#import "MinChart.h"
#import "KLineChart.h"
#import "HZLandscapeWuDangView.h"
#import "HZLandscapeIndexView.h"
#import "MAModifyView.h"

@interface HZLandscapeChartView()
@property (nonatomic, strong)HZLandscapeSummaryView *pSummaryView;
@property (nonatomic, strong)MinChart      *pMinChart;
@property (nonatomic, strong)KLineChart    *pKlineChart;
@property (nonatomic, strong)MYRadioGroup  *pRadioGroup;
@property (nonatomic, strong)HZLandscapeWuDangView *pWuDangView;
@property (nonatomic, strong)UIView *pCurrentShowView;
@property (nonatomic, strong)UIButton *pZhouQiBtn;
@property (nonatomic, strong)UIButton *pIndexBtn;

@property (nonatomic, strong)HZMinPriceModel *pMinPriceModel;
@property (nonatomic, strong)HZMinPriceModel *pKPriceModel;

@property (nonatomic, strong)HZLandscapeIndexView *pIndexView;
@property (nonatomic, assign)int nIndexState;

@property (nonatomic, strong)MAModifyView                  *pMaModifyView;//MA值修改的弹出视图
@end

@implementation HZLandscapeChartView
- (MAModifyView *)pMaModifyView
{
    if (!_pMaModifyView) {
        _pMaModifyView = [[MAModifyView alloc]initWithFrame:CGRectMake((self.frame.size.width - 240)/2, (self.frame.size.height-265)/2, 240, 265)];
        _pMaModifyView.parent = self;
        [self addSubview:_pMaModifyView];
    }
    return _pMaModifyView;
}
- (HZLandscapeIndexView *)pIndexView {
    if (!_pIndexView) {
        _pIndexView = [HZLandscapeIndexView new];
        _pIndexView.parent = self;
        [_pRadioGroup addSubview:_pIndexView];
        [_pIndexView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.mas_equalTo(_pRadioGroup);
            make.width.mas_equalTo(0);
        }];
    }
    return _pIndexView;
}
- (HZLandscapeWuDangView *)pWuDangView
{
    if (!_pWuDangView) {
        _pWuDangView = [HZLandscapeWuDangView new];
        _pWuDangView.layer.borderWidth = 0.5f;
        _pWuDangView.layer.borderColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil].CGColor;
        [self addSubview:_pWuDangView];
        [_pWuDangView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pSummaryView.mas_bottom);
            make.right.mas_equalTo(_pSummaryView).offset(-5);
            make.bottom.mas_equalTo(_pRadioGroup.mas_top);
            make.width.mas_equalTo(95);
        }];
    }
    return _pWuDangView;
}
- (UIView *)p_loadMinChartView
{
    if (!_pMinChart) {
        _pMinChart = [MinChart new];
        _pMinChart.parent = self;
        [_pMinChart setMode:MIN_SHOW_MODE_NORMAL];
        [_pMinChart setLandScapeBtnHidden:YES];
        [self addSubview:_pMinChart];
        [_pMinChart mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pSummaryView.mas_bottom);
            make.left.mas_equalTo(_pSummaryView);
            make.right.mas_equalTo(self.pWuDangView.mas_left);
            make.bottom.mas_equalTo(_pRadioGroup.mas_top);
        }];
    }
    _pKlineChart.hidden = YES;
    _pMinChart.hidden = NO;
    _pWuDangView.hidden = NO;
    return _pMinChart;
}

- (UIView *)p_loadKLineChartView
{
    if (!_pKlineChart) {
        _pKlineChart = [KLineChart new];
        _pKlineChart.parent = self;
        _pKlineChart.hidden = YES;
        [_pKlineChart setMode:KLINE_MODE_NORMAL];
        [_pKlineChart setLandScapeBtnHidden:YES];
        [self addSubview:_pKlineChart];
        [_pKlineChart mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pSummaryView.mas_bottom);
            make.left.mas_equalTo(_pSummaryView);
            make.right.mas_equalTo(_pSummaryView);
            make.bottom.mas_equalTo(_pRadioGroup.mas_top);
        }];
    }
    _pMinChart.hidden = _pWuDangView.hidden = YES;
    _pKlineChart.hidden = NO;
    return _pKlineChart;
}


#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}

#pragma mark -
#pragma mark public
- (void)updateMinData
{
    [_pMinChart updateNewData];
}
- (void)updateKLineData
{
    [_pKlineChart updateNewData];
}
- (void)updateNewDataWithStateTitle:(NSString *)title
{
    if ([title isEqual:@"分时"]) {
        [self p_updateIndexBtnWeight:0];
        [self p_loadMinChartView];
    }else {
        [self p_updateIndexBtnWeight:45];
        [self p_loadKLineChartView];
    }
    [self.superview setNeedsLayout];
    [self.superview layoutIfNeeded];
    
    
    if ([title isEqual:@"分时"]) {
        [self updateMinData];
        [_pRadioGroup setSelectedButton:0];
    }else if ([title isEqual:@"日K"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:1];
    }else if ([title isEqual:@"月K"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:2];
    }else if ([title isEqual:@"周K"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:3];
    }else if ([title isEqual:@"5分钟"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:4];
    }else if ([title isEqual:@"15分钟"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:5];
    }else if ([title isEqual:@"30分钟"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:6];
    }else if ([title isEqual:@"60分钟"]) {
        [self updateKLineData];
        [_pRadioGroup setSelectedButton:7];
    }
    
}
- (void)updateIndexWithState:(int)state
{
    _nIndexState = state;
    [_pKlineChart setCurShowIndex:state];
}
- (void)updatePriceData:(HZMinPriceModel *)priceData fromType:(KFromType)fromType
{
    if (fromType == KFromMinType) {
        _pMinPriceModel = priceData;
    }else if (fromType == KFromKLineType) {
        _pKPriceModel = priceData;
    }
    
    if ([[_pRadioGroup getSelectedButton].currentTitle isEqual:@"分时"]) {
        [_pSummaryView updateNewData:_pMinPriceModel fromType:KFromMinType];
        [_pWuDangView updateNewData:_pMinPriceModel];
    }else {
        [_pSummaryView updateNewData:_pKPriceModel fromType:KFromKLineType];
    }
}
- (void)updatePriceData:(HZMinPriceModel *)minPriceData kPriceData:(HZMinPriceModel *)kPriceData
{
    _pMinPriceModel = minPriceData;
    _pKPriceModel = kPriceData;
    if ([[_pRadioGroup getSelectedButton].currentTitle isEqual:@"分时"]) {
        [_pSummaryView updateNewData:_pMinPriceModel fromType:KFromMinType];
        [_pWuDangView updateNewData:_pMinPriceModel];
    }else {
        [_pSummaryView updateNewData:_pKPriceModel fromType:KFromKLineType];
    }
}
- (void)updateOwnModel:(HZOwnedStockModel *)ownModel
{
    [_pSummaryView updateOwnModel:ownModel];
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pSummaryView = [HZLandscapeSummaryView new];
    _pSummaryView.parent = self;
    
    _pZhouQiBtn = [UIButton new];
    _pIndexBtn = [UIButton new];
    [_pZhouQiBtn setTitle:@"周期" forState:UIControlStateNormal];
    [_pZhouQiBtn setBackgroundColor:kMYHexToRGB(0x385694, 1)];
    [_pZhouQiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_pZhouQiBtn addTarget:self action:@selector(zhouqiBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [_pIndexBtn setTitle:@"指标" forState:UIControlStateNormal];
    [_pIndexBtn setBackgroundColor:kMYHexToRGB(0x385694, 1)];
    [_pIndexBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_pIndexBtn addTarget:self action:@selector(indexBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pRadioGroup = [MYRadioGroup new];
    _pRadioGroup.parent = self;
    _pRadioGroup.backgroundColor = [HZCommonTool getThemeColorWithDefaultColor:kMYHexToRGB(0xf5f5f5, 1)];
    NSArray *titleArray = [NSArray arrayWithObjects:
                           kHZFenShi,
                           kHZDayK,
                           kHZWeekK,
                           kHZMonthK,
                           kHZMinute5,
                           kHZMinute15,
                           kHZMinute30,
                           kHZMinute60,nil];
    [_pRadioGroup setRadioGroup:titleArray];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
    [_pRadioGroup setSelectedButton:0];
    [_pRadioGroup resetAllButtonFont:[UIFont systemFontOfSize:15]];
    
    [self addSubview:_pSummaryView];
    [self addSubview:_pRadioGroup];
    [self addSubview:_pZhouQiBtn];
    [self addSubview:_pIndexBtn];
    
    [self p_initConstraints];
    
    _pCurrentShowView = [self p_loadMinChartView];
}

- (void)p_initConstraints
{
    if (IPHONE_X) {
        [_pSummaryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self).offset(30);
            make.right.mas_equalTo(self).offset(-34);
            make.height.mas_equalTo(50);
        }];
        
        [_pZhouQiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.left.mas_equalTo(self).offset(30);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(45);
        }];
        [_pIndexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.right.mas_equalTo(self).offset(-34);
            make.height.mas_equalTo(_pZhouQiBtn);
            make.width.mas_equalTo(0);
        }];
    }else {
        [_pSummaryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(50);
        }];
        [_pZhouQiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(45);
        }];
        [_pIndexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(_pZhouQiBtn);
            make.width.mas_equalTo(0);
        }];
    }
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.mas_equalTo(_pZhouQiBtn.mas_right);
        make.right.mas_equalTo(_pIndexBtn.mas_left);
        make.height.mas_equalTo(_pZhouQiBtn);
    }];
}
- (void)p_updateIndexBtnWeight:(CGFloat)w
{
    [_pIndexBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(w);
    }];
}
- (void)zhouqiBtnOnClick
{
    [self.pIndexView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0);
    }];
}
- (void)indexBtnOnClick
{
    [self.pIndexView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(CGRectGetWidth(_pRadioGroup.frame));
    }];
    [_pIndexView updateSelectedButton:_nIndexState];
}
#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_cancelLandscapeScreen_Action:
        {
            NSArray *params = @[[_pRadioGroup getSelectedButton].currentTitle,@([_pKlineChart getShowIndex])];
            [self.parent upToParentWithObjc:objc withType:type withParamter:params];
        }
            break;
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                
                if ([btn.currentTitle isEqual:kHZFenShi]) {
                    [self p_loadMinChartView];
                    [_pMinChart updateNewData];
                    [self p_updateIndexBtnWeight:0];
                    [_pSummaryView updateNewData:_pMinPriceModel fromType:KFromMinType];
                    [_pWuDangView updateNewData:_pMinPriceModel];
                }else {
                    [self p_loadKLineChartView];
                    [self updateIndexWithState:_nIndexState];
                    [self p_updateIndexBtnWeight:45];
                    [_pSummaryView updateNewData:_pKPriceModel fromType:KFromKLineType];
                }
                
                //发送日K、周K，月K
                int nKType = -1;
                if ([btn.currentTitle isEqual:kHZDayK]) {
                    nKType = 33;
                }else if ([btn.currentTitle isEqual:kHZWeekK]) {
                    nKType = 39;
                }else if ([btn.currentTitle isEqual:kHZMonthK]) {
                    nKType = 40;
                }else if ([btn.currentTitle isEqual:kHZMinute5]) {
                    nKType = 35;
                }else if ([btn.currentTitle isEqual:kHZMinute15]) {
                    nKType = 36;
                }else if ([btn.currentTitle isEqual:kHZMinute30]) {
                    nKType = 37;
                }else if ([btn.currentTitle isEqual:kHZMinute60]) {
                    nKType = 38;
                }
                if (nKType > 0) {
                    [self.parent upToParentWithObjc:self withType:kHZ_ClickKLineRequest_Action withParamter:@(nKType)];
                }
            }
            
        }
            break;
        case KMSClickIsShowTenLineActionType:
        {
            HZMinPriceModel *model = (HZMinPriceModel *)paramter;
            [_pSummaryView updateNewData:model fromType:KFromKLineType];
        }
            break;
        case KMSClickLandscapeIndexActionType:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                [_pKlineChart setSubChartIndex:btn.tag];
            }
        }
            break;
        case KMSKlineChartViewType:
        {
            if(paramter && [paramter isKindOfClass:[NSString class]]){
                if([paramter isEqualToString:@"Change_MA"]){
                    [self.pMaModifyView readData];
                    self.pMaModifyView.hidden = NO;
                    return;
                }else if([paramter isEqualToString:@"Save_MA"]){
                    [_pMaModifyView readData];
                    [_pKlineChart updateNewData];
                    return;
                }
            }
            
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}

@end
